package com.adroiti.texasholdem.engine;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.adroiti.texasholdem.Card;
import com.adroiti.texasholdem.Color;
import com.adroiti.texasholdem.Deck;

public class HoldemDeck implements Deck {

	private List<Card> cards;
	private List<Card> startingCards;
	
	public HoldemDeck(){
		startingCards = initCards();
		setCards(startingCards);
		shuffle();
	}

	@Override
	public void shuffle() {
		Collections.shuffle(cards);
	}

	@Override
	public Card fetchCard() {
		try {
			return cards.get(0);
		} finally {
			cards.remove(0);
		}
	}

	@Override
	public Deck getNewDeck() {
		if (startingCards == null) {
			startingCards = initCards();
		}
		setCards(startingCards);
		shuffle();
		return this;
	}

	private void setCards(List<Card> cards) {
		this.cards = cards;
	}

	private List<Card> initCards() {

		List<Card> cards = new ArrayList<Card>();
		for (Color color : Color.values()) {
			for (int i = 2; i <= 14; i++) {
				cards.add(new SimpleCard(color, i));
			}
		}
		return cards;
	}
}
